package zzhyc.keystore

import android.os.Bundle
import android.util.Log
import android.view.KeyEvent
import androidx.appcompat.app.AppCompatActivity
import com.google.gson.Gson
import com.qq.e.ads.splash.SplashAD
import com.qq.e.ads.splash.SplashADListener
import com.qq.e.comm.util.AdError
import zzhyc.keystore.databinding.ActivityHlysplashBinding

private const val TAG = "HLYSplashActivity"

class HLYSplashActivity : AppCompatActivity() {

    private val binding by lazy { ActivityHlysplashBinding.inflate(layoutInflater) }
    private var canJump = false
    private var splashAD: SplashAD? = null
    private var logStr = StringBuilder()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        initSdk()
    }

    private fun initSdk() {
        splashAD = SplashAD(this, HLYKey.HLY_SPLASH_ID, object : SplashADListener {
            override fun onADDismissed() {  //广告关闭时调用
                setLogsView("onADDismissed: \n")
                startActivity()
                val ecpm = splashAD?.ecpm
                val extras = splashAD?.extraInfo
                setLogsView("onADLoaded/extraInfo: ${Gson().toJson(extras)} \n")
                setLogsView("开屏价格-- ${ecpm} \n")
                /**
                 * {"mp":-1,"is_reward_ad":false,"click_dev_extra":{},"request_id":"dyulphevn32oe01","token":"","exp_dev_extra":{}}
                 */
            }

            override fun onNoAD(p0: AdError?) {
                setLogsView("onNoAD: ${p0?.errorCode}，msg-- ${p0?.errorMsg} \n")
                startActivity()
            }

            override fun onADPresent() {    //广告成功展示时调用
                setLogsView("onADPresent:  \n")
            }

            override fun onADClicked() {    //广告被点击时调用
                setLogsView("onADClicked:  \n")
            }

            override fun onADTick(p0: Long) {
                setLogsView("onADTick: ${p0} \n")
            }

            override fun onADExposure() {   //广告曝光时调用
                setLogsView("onADExposure:  \n")
            }

            override fun onADLoaded(p0: Long) { //广告加载成功的回调
                setLogsView("onADLoaded: ${p0} \n")
            }
        })
        splashAD?.showFullScreenAd(binding.hlySplashFl)
    }

    override fun onResume() {
        super.onResume()
        if (canJump) {
            startActivity()
        }
    }

    override fun onPause() {
        super.onPause()
        canJump = true
    }

    fun startActivity() {
        HLYInfoActivity.startActivity(this, logStr.toString())
        finish()
    }

    private fun setLogsView(logs: String) {
        Log.i(TAG, logs)
        logStr.append(logs)
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_HOME) {
            return true
        }
        return super.onKeyDown(keyCode, event)
    }
}